package com.nativetalk.service.membsetmeal.search;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.course.TsNtCourseInfoDetail;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.setmeal.TdNtMembSetmealDetail;
import com.nativetalk.bean.member.setmeal.TdNtMembSetmealList;
import com.nativetalk.bean.setmeal.TsNtSetmealIndex;
import com.nativetalk.bean.website.TsNtWebSiteConfIndex;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtMembSetmealDao;
import com.nativetalk.dao.TdNtMemberDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/1/5.
 */
@Service
public class MembSetmealSearchServiceImpl implements MembSetmealSearchService {
    private static Logger logger = Logger.getLogger(MembSetmealSearchServiceImpl.class);
    @Resource
    private TdNtMembSetmealDao tdNtMembSetmealDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Override
    public RetInfo findMembSetmealByMembId(TdNtMember tdNtMember, int currentPage, int pageSize) {
        String logInfo = this.getClass().getName() + ":findMembSetmealByMembId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            if (tdNtMember!=null) {
                int totalPage;//总页数
                int totalRecord = tdNtMembSetmealDao.findTdNtMembSetmealListSize(tdNtMember.getMemb_id());//总条数
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    currentPage = 1;
                }
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("memb_id",tdNtMember.getMemb_id());
                map.put("currentPage",0);
                map.put("pageSize",pageSize);

                List<TdNtMembSetmealList> tdNtMembSetmealLists = tdNtMembSetmealDao.findTdNtMembSetmealListPage(map);
                if (tdNtMembSetmealLists == null || tdNtMembSetmealLists.size() <= 0) {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("MEMBSETMEAL.RESULT.NULL"));
                } else {


                    result.put("tdNtMembSetmealLists", tdNtMembSetmealLists);
                    result.put("totalPage", totalPage);
                    result.put("totalRecord", totalRecord);
                    result.put("currentPage", currentPage);

                }
            }

            List<TsNtSetmealIndex> tsNtSetmealIndices =  (List<TsNtSetmealIndex>) MemcachedUtils.get(MemcachedKey.tsNtSetmealIndex);

//            List<TsNtSetmealIndex> recommended = new ArrayList<>();
//            if (tsNtSetmealIndices != null) {
//                if (tsNtSetmealIndices.size() > 2){
//                    recommended = tsNtSetmealIndices.subList(0,2);
//                } else {
//                    recommended = tsNtSetmealIndices;
//                }
//            }
            result.put("setmeals", tsNtSetmealIndices);
            retInfo.setObj(result);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取套餐列表成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findfinishMembSetmealByMembId(Integer memb_id, int currentPage, int pageSize) {
        String logInfo = this.getClass().getName() + ":findfinishMembSetmealByMembId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int totalPage;//总页数
            int totalRecord = tdNtMembSetmealDao.findFinsihTdNtMembSetmealListSize(memb_id);//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("memb_id",memb_id);
            map.put("currentPage",0);
            map.put("pageSize",pageSize);

            List<TdNtMembSetmealList> tdNtMembSetmealLists = tdNtMembSetmealDao.findFinsihTdNtMembSetmealListPage(map);
            if (tdNtMembSetmealLists == null || tdNtMembSetmealLists.size() <= 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("MEMBSETMEAL.RESULT.NULL"));
            } else {
                Map<String, Object> result = new HashMap<>();
                result.put("tdNtMembSetmealLists", tdNtMembSetmealLists);
                result.put("totalPage", totalPage);
                result.put("totalRecord", totalRecord);
                result.put("currentPage", currentPage);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取已完成套餐列表成功");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findMembSetmealByMembIdCouId(Integer memb_id, int cou_id) {
        String logInfo = this.getClass().getName() + ":findMembSetmealByMembIdCouId:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            BigDecimal balance = tdNtMemberDao.findTdNtMemberBalabce(memb_id);
            Map<String , TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);

            TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id+"");
            Map<String,Object> map = new HashMap<>();
            map.put("memb_id",memb_id);
            map.put("cou_id",tsNtCourseInfoDetail.getCate_id());
            TdNtMembSetmealDetail  tdNtMembSetmealDetail = tdNtMembSetmealDao.findTdNtMembSetmealDetail(map);
            Map<String,Object> result = new HashMap<>();
            result.put("banlance",balance);
            if (tdNtMembSetmealDetail!=null) {
                tdNtMembSetmealDetail.setSet_time(tdNtMembSetmealDetail.getSurplus_time());
            }

            result.put("membSetmeal",tdNtMembSetmealDetail);
            Map<String , TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String , TsNtWebSiteConfIndex>)MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            double sutdy_amount = Double.valueOf(tsNtWebSiteConfIndexMap.get("25").getData_value());
            result.put("amount", sutdy_amount);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(result);

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public TdNtMembSetmealDetail findMembSetmealByMembIdCouIdTwo(Integer memb_id, int cou_id) {
        String logInfo = this.getClass().getName() + ":findMembSetmealByMembIdCouIdTwo:";
        logger.info("======" + logInfo + "begin======");
        TdNtMembSetmealDetail  tdNtMembSetmealDetail = null;
        try {
            if (cou_id != 0) {
                Map<String , TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);

                TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id+"");
                Map<String,Object> map = new HashMap<>();
                map.put("memb_id",memb_id);
                map.put("cou_id",tsNtCourseInfoDetail.getCate_id());
                tdNtMembSetmealDetail  = tdNtMembSetmealDao.findTdNtMembSetmealDetail(map);
                if (tdNtMembSetmealDetail!=null) {
                    tdNtMembSetmealDetail.setSet_time(tdNtMembSetmealDetail.getSurplus_time());
                }
            } else {
                Map<String,Object> map = new HashMap<>();
                map.put("memb_id",memb_id);
                map.put("cou_id",cou_id);
                tdNtMembSetmealDetail  = tdNtMembSetmealDao.findTdNtMembSetmealDetail(map);
                if (tdNtMembSetmealDetail!=null) {
                    tdNtMembSetmealDetail.setSet_time(tdNtMembSetmealDetail.getSurplus_time());
                }
            }

        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return tdNtMembSetmealDetail;
    }
}
